define(['jquery', 'bootstrap', 'backend', 'table', 'form','range.min'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ques/volume/index',
                    add_url: 'ques/volume/add',
                    edit_url: 'ques/volume/edit',
                    del_url: 'ques/volume/del',
                    detail_url: 'ques/volume/detail',
                    multi_url: 'ques/volume/multi',
                    table: 'ques_volume',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status_school', title: __('Status_school'), searchList: {"1":__('Status_school 1'),"2":__('Status_school 2')}, visible: false, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name')},
                        //通过Ajax渲染searchList，也可以使用JSON数据

                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('zStatus 1'),"2":__('zStatus 2')}, formatter: Table.api.formatter.normal},
                        {field: 'desc', title: __('Desc')},
                        {field: 'publish_text', title: __('Publish'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'ok_rate', title: __('Ok_rate')},
                          {
                            field: 'url',
                            title: __('Url'),
                            align: 'left',
                            formatter: Controller.api.formatter.url
                        },
                        {
                            field: 'titles',
                            title: '数据同步',
                            align: 'left',
                            formatter: Controller.api.formatter.asyncque
                        },

                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'title',
                            width: "120px",
                            title: __('信息管理'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [

                                {
                                    name: 'detail',
                                    title: __('问题列表'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-leanpub',
                                    url: 'ques/subject/index',
                                    extend:'data-area=\'["80%","80%"]\'',
                                    callback: function (data) {
                                        console.log(data);
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                  name: 'detail',
                                  title: __('学校信息'),
                                  extend:"data-area='[\"70%\",\"70%\"]'",
                                  classname: 'btn btn-xs btn-primary btn-dialog',
                                  icon: 'fa fa-bank',
                                  url: 'ques/school/index',
                                  hidden:function (row) {
                                    if(row.status_school == 2)
                                    {
                                      return false;
                                    }
                                    return true;
                              }
                            },

                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'operate',
                            width: "120px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $('.single-slider').jRange({
                from: 0,
                to: 100,
                step: 1,
                scale: [0, 100],
                format: '%s',
                width: '100%',
                showLabels: true,
                showScale: true
            });
        },
        edit: function () {
            Controller.api.bindevent();
              $('.single-slider').jRange({
                from: 0,
                to: 100,
                step: 1,
                scale: [0, 100],
                format: '%s',
                width: '100%',
                showLabels: true,
                showScale: true
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm"><span class="input-group-btn input-group-sm"><a href="/index/ques/volume/ids/'+row.id+ '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                asyncque: function (value, row, index) {
                    return '<div class="input-group input-group-sm"><span class="input-group-btn input-group-sm"><a href="/admin/ajax/asyncque/ids/'+row.id+ '"  class="btn btn-default btn-sm btn-ajax"><i class="fa fa-link"></i></a></span></div>';
                },
                zdyname: function (value, row, index) {
                    var table = this.table;
                    // 默认按钮组
                    var buttons = [];
                    buttons.push({
                                name: 'detail',
                                title: row.name,
                                text: row.name,
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-dialog',
                                url: 'finance/extract/week/view',
                                extend:'data-area=\'["80%","80%"]\'',
                            });
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                },

          },
        }
    };
    return Controller;
});